package com.alinesno.cloud.base.logger.alarm.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alinesno.cloud.base.logger.alarm.AlarmService;
import com.alinesno.cloud.base.logger.alarm.constants.AlarmTypeConstants;
import com.alinesno.cloud.base.logger.entity.LogSystemExceptionEntity;
import com.alinesno.cloud.base.logger.entity.LogTriggerConditionEntity;
import com.alinesno.cloud.base.logger.entity.LogTriggerEntity;
import com.alinesno.cloud.base.logger.service.ILogSystemExceptionService;
import com.alinesno.cloud.base.notice.entity.EmailSendEntity;
import com.alinesno.cloud.base.notice.entity.SmsSendEntity;
import com.alinesno.cloud.common.facade.wrapper.RestWrapper;

/**
 * 方法异常错误 
 * 
 * @author WeiXiaoJin
 * @since 2020年1月29日 下午19:45:08
 */
@Component(AlarmTypeConstants.ALARM_METHOD_EXCEPTION + AlarmTypeConstants.SERVICE_SUFFIX)
public class MethodExceptionAlarmServiceImpl extends AlarmService {

	@Autowired
	private ILogSystemExceptionService exceptionService ; 
	
	private List<LogSystemExceptionEntity> es = null;
	
	@Override
	public boolean doAlarm(LogTriggerEntity e , List<LogTriggerConditionEntity> cList) {
		RestWrapper restWrapper = buildFilterCondition(cList) ; 
		es = exceptionService.findAll(restWrapper);
		cs = cList ; 

		return es.size() > 0 ? true : false;
	}

	@Override
	protected EmailSendEntity generatorEmail(LogTriggerEntity e) {
		if(StringUtils.isBlank(e.getNoticeEmail())) {
			log.warn(e.getTriggerName() + " 预警接收邮件接收者为空.");
			return null ; 
		}
		String subTitle = "应用【"+e.getApplicationName()+"】系统异常信息：" + e.getTriggerName() ; 
		
		StringBuffer sb = new StringBuffer() ; 
		sb.append("<b>异常信息</b>"+NR) ;
		sb.append("应用名:" + e.getApplicationName() + NR) ; 
		
		if(es.size() > EMAIL_ERROR_DEFAULT_LENGTH) {
			sb.append("异常项:<b>" + es.size() +"</b>"+NR) ;
		}
		
		for(int i = 0 ; i < (es.size()>EMAIL_ERROR_DEFAULT_LENGTH?EMAIL_ERROR_DEFAULT_LENGTH:es.size()) ; i ++) {
			LogSystemExceptionEntity b = es.get(i) ; 
			sb.append("异常类:" + b.getMethodDesc()+":" + b.getMethod() + NR) ;
			sb.append("调用信息:" + b.getErrorMessage() + NR) ;
			sb.append("异常次数:" + b.getErrorCount() + NR) ;
			sb.append("最后异常:" + b.getUpdateTime() + NR) ;
		}
		sb.append(NR) ; 
		if(es.size() > EMAIL_ERROR_DEFAULT_LENGTH) {
			sb.append("更多 ...... " +NR) ;
		}
		sb.append(NR) ; 
		sb.append("<b>监控项</b>"+NR) ;
		for(LogTriggerConditionEntity b : cs) {
			sb.append(b.getConditionName() + ":" + b.getConditionValue() + NR) ;
		}
		
		EmailSendEntity email = new EmailSendEntity() ; 
		email.setEmailSubject(subTitle);
		email.setEmailContent(sb.toString());
		email.setEmailReceiver(e.getNoticeEmail()); 
		
		return email ;
	}

	@Override
	protected SmsSendEntity generatorSms(LogTriggerEntity e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void generatorWechat(LogTriggerEntity e) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void generatorDingtalk(LogTriggerEntity e) {
		// TODO Auto-generated method stub

	}

}
